﻿Imports System.Drawing
Imports System.Drawing.Drawing2D

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class VerticalLabel
    Inherits System.Windows.Forms.Label

#Region "Designer Generated Code"
    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private WithEvents components As System.Windows.Forms.Label

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'components
        '
        Me.components.Location = New System.Drawing.Point(0, 0)
        Me.components.Name = "components"
        Me.components.Size = New System.Drawing.Size(100, 23)
        Me.components.TabIndex = 0
        Me.components.AutoSize = False
        '
        'VerticalContainerLabel
        '
        Me.Name = "VerticalContainerLabel"
        Me.ResumeLayout(False)
    End Sub
#End Region

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)

    End Sub

    Private iVerticalText As Boolean = True
    Public Property VerticalText As Boolean
        Get
            Return Me.iVerticalText
        End Get
        Set(value As Boolean)
            Me.iVerticalText = value
            FixUpSizeRatio()
        End Set
    End Property
    Private iTextRotationAngle As Single = 270
    Public Property TextRotationAngle As Single
        Get
            Return Me.iTextRotationAngle
        End Get
        Set(value As Single)
            Me.iTextRotationAngle = value
            Me.Invalidate()
        End Set
    End Property
    Private iMouseOver As Boolean = False
    Private iBorders As Boolean = True
    Public Property Borders As Boolean
        Get
            Return Me.iBorders
        End Get
        Set(value As Boolean)
            Me.iBorders = value
            Me.Invalidate()
        End Set
    End Property
    Private iHover As Boolean = True
    Public Property Hover As Boolean
        Get
            Return Me.iHover
        End Get
        Set(value As Boolean)
            Me.iHover = value
            Me.Invalidate()
        End Set
    End Property
    Private iControlBackColorHover As Color = SystemColors.Info
    Public Property ControlBackColorHover As Color
        Get
            Return Me.iControlBackColorHover
        End Get
        Set(value As Color)
            Me.iControlBackColorHover = value
            Me.Invalidate()
        End Set
    End Property
    Private iControlBackColorNormal As Color = SystemColors.InactiveBorder
    Public Property ControlBackColorNormal As Color
        Get
            Return Me.iControlBackColorNormal
        End Get
        Set(value As Color)
            Me.iControlBackColorNormal = value
            Me.Invalidate()
        End Set
    End Property
    Dim iParentBackColor As Color = SystemColors.GradientInactiveCaption
    Public Property ParentBackColor As Color
        Get
            Return Me.iParentBackColor
        End Get
        Set(value As Color)
            Me.iParentBackColor = value
            Me.Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaintBackground(pevent As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaintBackground(pevent)
        pevent.Graphics.Clear(Me.iParentBackColor)
    End Sub

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)

        'Dim sf As New Drawing.StringFormat
        'sf.FormatFlags = StringFormatFlags.DirectionVertical + StringFormatFlags.MeasureTrailingSpaces
        'sf.Alignment = StringAlignment.Center
        ''Dim measuredSize As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font, Me.Location, sf)
        ''Me.Size = measuredSize.ToSize
        'e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle, sf)

        'erase background
        e.Graphics.FillRectangle(New SolidBrush(Me.iParentBackColor), Me.ClientRectangle)

        Dim aRect As New Rectangle(Me.ClientRectangle.X, _
                                   Me.ClientRectangle.Y, _
                                   Me.ClientRectangle.Width - 1, _
                                   Me.ClientRectangle.Height - 1)
        Dim gh As New GraphicsHelper
        Dim bpath As GraphicsPath = gh.GetRoundCornerPathForRectangle(aRect)

        'fill background
        Dim backBrush As Brush = Nothing
        If (Me.iHover = True) _
            AndAlso (Me.iMouseOver = True) Then
            backBrush = New SolidBrush(Me.iControlBackColorHover)
        Else
            backBrush = New SolidBrush(Me.iControlBackColorNormal)
        End If
        e.Graphics.FillPath(backBrush, bpath)

        'draw text
        If Me.iVerticalText = False Then
            Dim sf As StringFormat = gh.GetTextFormatObjectFromContentAlignment(Me.TextAlign)
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), aRect, sf)
        Else
            Dim sf As StringFormat = gh.GetTextFormatObjectFromContentAlignment(Me.TextAlign)
            e.Graphics.RotateTransform(Me.iTextRotationAngle)
            e.Graphics.TranslateTransform(Me.ClientSize.Width / 2, _
                                          Me.ClientSize.Height / 2, _
                                          Drawing2D.MatrixOrder.Append)
            e.Graphics.DrawString(Me.Text, Me.Font, Brushes.Black, 0, 0, sf)
            e.Graphics.ResetTransform()
        End If

        'draw borders
        If Me.iBorders Then
            Dim borderPen As Pen = SystemPens.ActiveBorder
            e.Graphics.DrawPath(borderPen, bpath)
        End If

    End Sub

    Protected Overrides Sub OnResize(e As System.EventArgs)
        MyBase.OnResize(e)
        FixUpSizeRatio()
    End Sub

    Protected Overrides Sub OnLayout(levent As System.Windows.Forms.LayoutEventArgs)
        MyBase.OnLayout(levent)
        FixUpSizeRatio
    End Sub

    Private Sub FixUpSizeRatio()
        If Me.iVerticalText Then
            If Me.Width > Me.Height Then
                Me.Size = New Size(Me.Height, Me.Width)
            End If
        Else
            If Me.Width < Me.Height Then
                Me.Size = New Size(Me.Height, Me.Width)
            End If
        End If
    End Sub

    Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
        MyBase.OnMouseEnter(e)
        If Me.iMouseOver = False Then
            Me.iMouseOver = True
            If Me.iHover = True Then Me.Invalidate()
        End If
    End Sub

    Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        If Me.iMouseOver = True Then
            Me.iMouseOver = False
            If Me.iHover = True Then Me.Invalidate()
        End If
    End Sub

End Class
